package org.example.cicdi.utils;

import com.google.gson.Gson;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 请求工具类
 */
public class HttpUtils {
	
	private static final CloseableHttpClient httpclient = HttpClients.createDefault();
	
	private static Map<String, String>defaultHeaderMap = new HashMap<String, String>();
	
	static {
		defaultHeaderMap.put("Content-Type", "application/json;charset=UTF-8");
	}
	
	public static String sendPost(String url) throws Exception {
		return sendPost(url, defaultHeaderMap, null);
	}
	
	public static String sendPost(String url, Map<String, Object>paramMap) throws Exception {
		return sendPost(url, defaultHeaderMap, paramMap);
	}
	
	public static String sendPost(String url, Object obj) throws Exception {
		return sendPost(url, defaultHeaderMap, obj);
	}
	
	public static String sendPost(String url, Map<String, String>headerMap, Object obj) throws Exception {
        //请求地址设置
        HttpPost httpPost = new HttpPost(url);
        // 设置超时时间
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(50000000).setConnectionRequestTimeout(50000000)
                .setSocketTimeout(50000000).build();
        httpPost.setConfig(requestConfig);

        //请求头设置
        if (headerMap != null  && CollectionUtils.isNotEmpty(headerMap.keySet())) {
        	for (String key : headerMap.keySet()) {
        		httpPost.setHeader(key, headerMap.get(key));
        	}
        }
        
        //请求参数设置
        if (obj != null) {
            String json = new Gson().toJson(obj);
        	httpPost.setEntity(new StringEntity(json));
            httpPost.setEntity(new StringEntity(new Gson().toJson(obj),"utf-8"));
        }
        
        CloseableHttpResponse response = null;
        String result = null;
        try {
            // 执行post请求
            response = httpclient.execute(httpPost);
            // 得到entity
            HttpEntity entity = response.getEntity();
            // 得到字符串
            result = EntityUtils.toString(entity);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                	 
                }
            }
        }
        
        return result;
	}
	
	public static String sendPost(String url, Map<String, String>headerMap, Map<String,Object>paramMap) throws Exception {
        //请求地址设置
        HttpPost httpPost = new HttpPost(url);
        
        //请求头设置
        if (headerMap != null  && CollectionUtils.isNotEmpty(headerMap.keySet())) {
        	for (String key : headerMap.keySet()) {
        		httpPost.setHeader(key, headerMap.get(key));
        	}
        }
        
        //请求参数设置
        if (paramMap != null && CollectionUtils.isNotEmpty(paramMap.keySet())) {
        	httpPost.setEntity(new StringEntity(new Gson().toJson(paramMap)));
        }
        
        CloseableHttpResponse response = null;
        String result = null;
        try {
            // 执行post请求
            response = httpclient.execute(httpPost);
            // 得到entity
            HttpEntity entity = response.getEntity();
            // 得到字符串
            result = EntityUtils.toString(entity);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                	 
                }
            }
        }
        
        return result;
	}
	
	public static String sendGet(String url) throws Exception {
        //请求地址设置
        HttpGet httpPost = new HttpGet(url);
        
        CloseableHttpResponse response = null;
        String result = null;
        try {
            // 执行post请求
            response = httpclient.execute(httpPost);
            // 得到entity
            HttpEntity entity = response.getEntity();
            // 得到字符串
            result = EntityUtils.toString(entity);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                	 
                }
            }
        }
        
        return result;
	}
}
